package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Role;
import com.wn.common.pojo.Rsp;
import com.wn.mapper.RoleMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 添加新角色
     * @param role
     * @return
     */
    public Rsp insertRole(Role role){
       int exe =  roleMapper.insert(role);
       if(exe>0)
           return Rsp.success(exe).msg("添加成功！");
       else
           return Rsp.fail().msg("添加失败！");
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    public  Rsp updateRole(Role role){
        int exe = roleMapper.updateByPrimaryKeySelective(role);
        if(exe>0)
            return Rsp.success(exe).msg("修改成功！");
        else
            return Rsp.fail().msg("修改失败！");
    }

    /**
     * 删除角色
     * @param role
     * @return
     */
    public Rsp deleteRole(Role role) {
        int exe = roleMapper.deleteByPrimaryKey(role.getRoleId(), role.getUpdateUserId());
        if (exe > 0)
            return Rsp.success(exe).msg("删除成功！");
        else
            return Rsp.fail().msg("删除失败！");

    }

    /**
     * 获取角色列表
     * @param pageParam
     * @return
     */

    public Rsp getRoleList(PageParam<Role> pageParam){
        if(pageParam.getSuperSearchKeyWord()!=null&& !StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<Role> roleList = roleMapper.selectBySuperSearchKeyWord(pageParam.getSuperSearchKeyWord());
            PageInfo<Role> pageInfo = new PageInfo<>(roleList);
            if(roleList!=null)
                return Rsp.success(pageInfo).msg("查询成功！");
            else
                return Rsp.fail().msg("查询失败！");
        }
        else{
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            List<Role> roleList = roleMapper.selectAll();
            PageInfo<Role> pageInfo = new PageInfo<>(roleList);
            if(roleList!=null)
                return Rsp.success(pageInfo).msg("查询成功！");
            else
                return Rsp.fail().msg("查询失败！");
        }

    }

    /**
     * 查询单个角色
     * @param roleId
     * @return
     */
    public Rsp getRoleByPrimaryKey(Integer roleId){
        Role role = roleMapper.selectByPrimaryKey(roleId);
        if(role!=null)
            return Rsp.success(role).msg("查询成功！");
        else
            return Rsp.fail().msg("查询失败!");
    }

    public Rsp updateStatus(Role role){
        if(role.getRoleStatus().equals("0"))
            role.setRoleStatus("1");
        else
            role.setRoleStatus("0");
        System.out.println("--------------"+role);
        int exe = roleMapper.updateByPrimaryKeySelective(role);
        if(exe>0)
            return Rsp.success(exe).msg("修改成功！");
        else
            return Rsp.fail().msg("修改失败！");
    }

}
